<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\home\controller;

use api\index\controller\CommonController;
use app\admin\model\ActivityModel;
use app\admin\model\CommonModel;
use app\admin\model\FavoriteModel;
use cmf\controller\RestBaseController;

//活动
class ActivityController extends RestBaseController
{
    //活动轮播列表
    public function activityRotationList(){
        if($this->request->isGet()){
            $where = ['is_rotation'=>ActivityModel::ROTATION_YES];
            $res = CommonController::selectWhereData('activity',$where,'id,title,thumbnail');
            foreach($res as &$value){
                $value['thumbnail'] = CommonController::absolutionUrl($value['thumbnail']);
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    //活动列表
    public function activityList(){
        if($this->request->isGet()){
            $page = $this->request->get('page');//分页页码
            $rule = config('verify.page');
            $this->checkValidate(['page'=>$page],$rule);
            $res = CommonController::selectPageData('activity',$page,'id,title,thumbnail,start_time,end_time,status','status asc');
           // $ages = array();
            foreach($res as &$value){
               // $ages[] = $value['status'];
                $value['thumbnail'] = CommonController::absolutionUrl($value['thumbnail']);
                $value['start_time'] = date('Y-m-d H:i',$value['start_time']);
                $value['end_time'] = date('Y-m-d H:i',$value['end_time']);
            }
           // array_multisort($ages, SORT_ASC, $res);
            $total_page = CommonController::totalPage('activity');
            $this->success('成功',['data'=>$res,'last_page'=>$total_page]);
        }else{
            $this->error('请求方式错误');
        }
    }

    //活动详情
    public function activityDetail(){
        if($this->request->isGet()){
            $activity_id = $this->request->get('activity_id');
            $rule = config('verify.activity_detail');
            $this->checkValidate(['activity_id'=>$activity_id],$rule);

            $res = CommonController::findWhereData('activity',['id'=>$activity_id],'id,title,start_time,end_time,read_num,praise_num,content');
            if($res){
                $res['start_time'] = date('Y-m-d H:i',$res['start_time']);
                $res['end_time'] = date('Y-m-d H:i',$res['end_time']);
                $commonModel = new CommonModel();
                $res['content'] = $commonModel->getPostContentAttr($res['content']);

                //是否点赞
                $member_id = $this->getUserId();
                $where = [
                    'member_id' => $member_id,
                    'type' => FavoriteModel::PRAISE,
                    'relation_type' => FavoriteModel::ACTIVITY_TYPE
                ];
                $res_praise = CommonController::selectWhereData('favorite',$where,'id,relation_id');
                $res['is_praise'] = FavoriteModel::NO;//否
                foreach($res_praise as $c_value){
                    if($res['id'] == $c_value['relation_id']){
                        $res['is_praise'] = FavoriteModel::YES;//是
                        break;
                    }
                }

                //是否收藏
                $where['type'] = FavoriteModel::COLLECTION;
                $res_collection = CommonController::selectWhereData('favorite',$where,'id,relation_id');
                $res['is_collection'] = FavoriteModel::NO;//否
                foreach($res_collection as $c_value){
                    if($res['id'] == $c_value['relation_id']){
                        $res['is_collection'] = FavoriteModel::YES;//是
                        break;
                    }
                }

                //增加阅读量
                ActivityModel::where('id',$res['id'])->setInc('read_num',1);
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }
}
